/**
 * 
 */
package com.carnation.qa.socketserver.proxy.moi;

import java.rmi.RemoteException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.carnation.metaone.mgr.exception.MOException;
import com.carnation.mong.platform.domain.MOIData;
import com.carnation.mong.platform.domain.MOINode;
import com.carnation.mong.platform.domain.MOIRelationType;
import com.carnation.mong.platform.exception.SessionException;
import com.carnation.mong.platform.service.DataService;
import com.carnation.qa.socketserver.domain.Item;
import com.carnation.qa.socketserver.proxy.Builder;

/**
 * @author Nick
 * 
 */
public class DataServiceImpl extends AbstractMOIServiceImpl {

	private final Log log = LogFactory.getLog(getClass());

	private DataService service;

	public DataServiceImpl(List<Item> items) {
		super(items);
		service = ServiceFactory.getService(DataService.class);
	}

	@Override
	protected String invoke() {
		Item item = getItems().get(0);
		String method = getMethodName(item);
		String returnStr;
		if ("create".equals(method)) {
			returnStr = create(item);
		} else if ("delete".equals(method)) {
			returnStr = delete(item);
		} else if ("getChildNodes".equals(method)) {
			returnStr = getChildNodes(item);
		} else if ("getChildren".equals(method)) {
			returnStr = getChildren(item);
		} else if ("getChildrenCount".equals(method)) {
			returnStr = getChildrenCount(item);
		} else if ("getData".equals(method)) {
			returnStr = getData(item);
		} else if ("getDataByPath".equals(method)) {
			returnStr = getDataByPath(item);
		} else if ("getDatas".equals(method)) {
			returnStr = getDatas(item);
		} else if ("getDatasCount".equals(method)) {
			returnStr = getDatasCount(item);
		} else if ("getParent".equals(method)) {
			returnStr = getParent(item);
		} else if ("getPath".equals(method)) {
			returnStr = getPath(item);
		} else if ("getRootNodes".equals(method)) {
			returnStr = getRootNodes(item);
		} else if ("getRoots".equals(method)) {
			returnStr = getRoots(item);
		} else if ("move".equals(method)) {
			returnStr = move(item);
		} else if ("update".equals(method)) {
			returnStr = update(item);
		} else {
			returnStr = new IllegalArgumentException(method).toString();
		}
		return returnStr;
	}

	private String create(Item item) {
		if (item.getChildren().size() == 1) {// 只有一个参数
			MOIData data = instanceMOIData(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("MOIData:" + data);
			}
			MOIData obj = null;
			try {
				obj = service.create(data);
				ServiceFactory.commit();
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String delete(Item item) {
		if (item.getChildren().size() == 1) {// 只有一个参数
			Item dataIdItem = item.getChildren().get(0);
			if (dataIdItem.getLabel().equals(MOIData.class.getSimpleName())) {
				MOIData data = instanceMOIData(dataIdItem);
				if (log.isDebugEnabled()) {
					log.debug("MOIData:" + data);
				}
				try {
					service.delete(data);
					return "true";
				} catch (Exception e) {
					if (log.isErrorEnabled()) {
						log.error(item.toString(), e);
					}
					return e.toString();
				}
			} else {
				String dataId = getString(dataIdItem.getLabelParts()[1]);
				if (log.isDebugEnabled()) {
					log.debug("dataId:" + dataId);
				}
				try {
					service.delete(dataId);
					return "true";
				} catch (Exception e) {
					if (log.isErrorEnabled()) {
						log.error(item.toString(), e);
					}
					return e.toString();
				}
			}
		}
		return new IllegalArgumentException(item.getLabel()).toString();
	}

	private String getChildNodes(Item item) {
		if (item.getChildren().size() == 1) {// 只有一个参数
			String dataId = getString(item.getChildren().get(0).getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("dataId:" + dataId);
			}
			try {
				List<MOINode> list = service.getChildNodes(dataId);
				if (log.isDebugEnabled()) {
					log.debug(list);
				}
				return new Builder(list, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		} else if (item.getChildren().size() == 3) {
			String dataId = getString(item.getChildren().get(0).getLabelParts()[1]);
			int start = getInteger(item.getChildren().get(1).getLabelParts()[1]);
			int count = getInteger(item.getChildren().get(2).getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("dataId:" + dataId + ",start:" + start + ",count:"
						+ count);
			}
			try {
				List<MOINode> list = service
						.getChildNodes(dataId, start, count);
				if (log.isDebugEnabled()) {
					log.debug(list);
				}
				return new Builder(list, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return new IllegalArgumentException(item.getLabel()).toString();
	}

	private String getChildren(Item item) {
		if (item.getChildren().size() == 1) {// 只有一个参数
			Item dataIdItem = item.getChildren().get(0);
			if (dataIdItem.getLabel().equals(MOIData.class.getSimpleName())) {
				MOIData data = instanceMOIData(dataIdItem);
				if (log.isDebugEnabled()) {
					log.debug("MOIData:" + data);
				}
				try {
					List<MOIData> list = service.getChildren(data);
					if (log.isDebugEnabled()) {
						log.debug(list);
					}
					return new Builder(list, null, item.getTabSeparator())
							.toString();
				} catch (Exception e) {
					if (log.isErrorEnabled()) {
						log.error(item.toString(), e);
					}
					return e.toString();
				}
			} else {
				String dataId = getString(dataIdItem.getLabelParts()[1]);
				if (log.isDebugEnabled()) {
					log.debug("dataId:" + dataId);
				}
				try {
					List<MOIData> list = service.getChildren(dataId);
					return new Builder(list, null, item.getTabSeparator())
							.toString();
				} catch (Exception e) {
					if (log.isErrorEnabled()) {
						log.error(item.toString(), e);
					}
					return e.toString();
				}
			}
		} else if (item.getChildren().size() == 3) {
			String dataId = getString(item.getChildren().get(0).getLabelParts()[1]);
			Integer start = getInteger(item.getChildren().get(1)
					.getLabelParts()[1]);
			Integer count = getInteger(item.getChildren().get(2)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("dataId:" + dataId + ",start:" + start + ",count:"
						+ count);
			}
			try {
				List<MOIData> list = service.getChildren(dataId, start, count);
				if (log.isDebugEnabled()) {
					log.debug(list);
				}
				return new Builder(list, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		} else if (item.getChildren().size() == 2) {
			String dataId = getString(item.getChildren().get(0).getLabelParts()[1]);
			if (item.getChildren().get(1).getLabel().equals(MOIRelationType.class.getSimpleName())) {
				Item rtItem = item.getChildren().get(1);
				MOIRelationType rt = instanceMOIRelationType(rtItem);
				if (log.isDebugEnabled()) {
					log.debug("dataId:[" + dataId + "], RelationType:[" + rt.toString() + "]");
				}
				try {
					List<MOIData> children = service.getChildren(dataId, rt);
					return new Builder(children, null, item.getTabSeparator()).toString();
				} catch (Exception e) {
					if (log.isErrorEnabled()) {
						log.error(item.toString(), e);
					}
					return e.toString();
				}
			}
		}
		return new IllegalArgumentException(item.getLabel()).toString();
	}

	private String getChildrenCount(Item item) {
		if (item.getChildren().size() == 1) {// 只有一个参数
			String dataId = getString(item.getChildren().get(0).getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("dataId:" + dataId);
			}
			try {
				Object obj = service.getChildrenCount(dataId);
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return new IllegalArgumentException(item.getLabel()).toString();
	}

	private String getData(Item item) {
		if (item.getChildren().size() == 1) {// 只有一个参数
			String id = getString(item.getChildren().get(0).getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("dataId:" + id);
			}
			try {
				Object obj = service.getData(id);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return new IllegalArgumentException(item.getLabel()).toString();
	}

	private String getDataByPath(Item item) {
		if (item.getChildren().size() == 1) {// 只有一个参数
			String path = getString(item.getChildren().get(0).getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("path:" + path);
			}
			try {
				Object obj = service.getDataByPath(path);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return new IllegalArgumentException(item.getLabel()).toString();
	}

	private String getDatas(Item item) {
		switch (item.getChildren().size()) {
		case 1: {
			String modelId = getString(item.getChildren().get(0)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("modelId:" + modelId);
			}
			try {
				Object obj = service.getDatas(modelId);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		case 2: {
			// TODO not yet implemented
			if (log.isWarnEnabled()) {
				log
						.warn("getDatas(String modelId,Criteria criteria) not yet implemented");
			}
			break;
		}
		case 3: {
			String modelId = getString(item.getChildren().get(0)
					.getLabelParts()[1]);
			Integer start = getInteger(item.getChildren().get(1)
					.getLabelParts()[1]);
			Integer count = getInteger(item.getChildren().get(2)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("modelId:" + modelId + ",start:" + start + ",count:"
						+ count);
			}
			try {
				Object obj = service.getDatas(modelId, start, count);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		}
		return new IllegalArgumentException(item.getLabel()).toString();
	}

	private String getDatasCount(Item item) {
		if (item.getChildren().size() == 1) {// 只有一个参数
			String modelId = getString(item.getChildren().get(0)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("modelId:" + modelId);
			}
			try {
				Object obj = service.getDatasCount(modelId);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		} else if (item.getChildren().size() == 2) {
			// TODO not yet implemented
			if (log.isWarnEnabled()) {
				log
						.warn("getDatasCount(String modelId,Criteria criteria) not yet implemented");
			}
		}
		return new IllegalArgumentException(item.getLabel()).toString();
	}

	private String getParent(Item item) {
		if (item.getChildren().size() == 1) {// 只有一个参数
			Item dataIdItem = item.getChildren().get(0);
			if (dataIdItem.getLabel().equals(MOIData.class.getSimpleName())) {
				MOIData data = instanceMOIData(dataIdItem);
				if (log.isDebugEnabled()) {
					log.debug("MOIData:" + data);
				}
				Object obj = null;
				try {
					obj = service.getParent(data);
					if (log.isDebugEnabled()) {
						log.debug(obj);
					}
					return new Builder(obj, null, item.getTabSeparator())
							.toString();
				} catch (Exception e) {
					if (log.isErrorEnabled()) {
						log.error(item.toString(), e);
					}
					return e.toString();
				}
			} else {
				String dataId = getString(dataIdItem.getLabelParts()[1]);
				if (log.isDebugEnabled()) {
					log.debug("dataId:" + dataId);
				}
				try {
					Object obj = service.getParent(dataId);
					if (log.isDebugEnabled()) {
						log.debug(obj);
					}
					return new Builder(obj, null, item.getTabSeparator())
							.toString();
				} catch (Exception e) {
					if (log.isErrorEnabled()) {
						log.error(item.toString(), e);
					}
					return e.toString();
				}
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String getPath(Item item) {
		if (item.getChildren().size() == 1) {// 只有一个参数
			MOIData data = instanceMOIData(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("MOIData:" + data);
			}
			Object obj = null;
			try {
				obj = service.getPath(data);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String getRootNodes(Item item) {
		Object obj = null;
		try {
			obj = service.getRootNodes();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

	private String getRoots(Item item) {
		Object obj = null;
		try {
			obj = service.getRoots();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

	private String move(Item item) {
		if (item.getChildren().size() == 2) {// 只有一个参数
			if (item.getChildren().get(0).getLabel().equals(
					MOIData.class.getName())) {
				MOIData thisData = instanceMOIData(item.getChildren().get(0));
				MOIData newParentData = instanceMOIData(item.getChildren().get(
						1));
				if (log.isDebugEnabled()) {
					log.debug("thisData:" + thisData + ",newParentData:"
							+ newParentData);
				}
				Object obj = null;
				try {
					obj = service.move(thisData, newParentData);
					if (log.isDebugEnabled()) {
						log.debug(obj);
					}
					return new Builder(obj, null, item.getTabSeparator())
							.toString();
				} catch (Exception e) {
					if (log.isErrorEnabled()) {
						log.error(item.toString(), e);
					}
					return e.toString();
				}
			} else {
				String thisDataId = item.getChildren().get(0).getLabelParts()[1];
				String newParentDataId = item.getChildren().get(1)
						.getLabelParts()[1];
				if (log.isDebugEnabled()) {
					log.debug("thisDataId:" + thisDataId + ",newParentDataId:"
							+ newParentDataId);
				}
				Object obj = null;
				try {
					obj = service.move(thisDataId, newParentDataId);
					if (log.isDebugEnabled()) {
						log.debug(obj);
					}
					return new Builder(obj, null, item.getTabSeparator())
							.toString();
				} catch (Exception e) {
					if (log.isErrorEnabled()) {
						log.error(item.toString(), e);
					}
					return e.toString();
				}
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

	private String update(Item item) {
		if (item.getChildren().size() == 1) {// 只有一个参数
			MOIData data = instanceMOIData(item.getChildren().get(0));
			if (log.isDebugEnabled()) {
				log.debug("MOIData:" + data);
			}
			Object obj = null;
			try {
				service.update(data);
				obj = service.getData(data.getId());
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		return IllegalArgumentException.class.getSimpleName();
	}

}
